<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta charset="utf-8">
    <title>会员账户编辑</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="bookmark" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/css/layui.css" media="all">
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/layui/layui.js"
            charset="utf-8"></script>
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/jquery/jquery.js"></script>
    <script type="application/javascript"
            src="${pageContext.request.contextPath}/private/js/loading/ajaxloading.js"></script>
</head>
<body>
<fieldset class="layui-elem-field">
    <legend>会员基本信息更新</legend>
    <form id="form1" class="layui-form" action="">
        <table width="96%" align="center">
            <tr height="23" style="display: none;">
                <td align="center">openid:</td>
                <td align="left">
                    <input type="hidden" id="userid" name="userid" value="${userInfo.userid}">
                    <input id="openid" name="openid" class="layui-input" type="text" placeholder="请输入登录账号"
                           autocomplete="off"
                           lay-verify="openid" value="${userInfo.openid}">
                </td>
            </tr>
            <tr height="23">
                <td align="center">用户名称:</td>
                <td align="left">
                    <input id="username" name="username" class="layui-input" type="text" placeholder="请输入用户名称"
                           autocomplete="off"
                           lay-verify="username" value="${userInfo.username}">
                </td>
            </tr>
            <tr height="23">
                <td align="center">性别:</td>
                <td align="left">
                    <input id="sex" name="sex" type="checkbox" lay-skin="switch"
                           value="${userInfo.sex}" ${userInfo.sex=="2"?"":"checked"}
                           lay-text="男|女">
                </td>
            </tr>
            <tr height="23">
                <td align="center">联系电话:</td>
                <td align="left">
                    <input id="telephone" name="telephone" class="layui-input" type="text" placeholder="请输入手机号码，不能超过11位"
                           autocomplete="off"
                           lay-verify="telephone" value="${userInfo.telephone}">
                </td>
            </tr>
            <tr height="23">
                <td align="center">课程顾问:</td>
                <td align="left">
                    <select id="addid" name="addid" lay-verify="addid">
                        <option value="">请选择</option>
                    </select>
                </td>
            </tr>
            <tr height="23">
                <td align="center">有效截止期:</td>
                <td align="left">
                    <input id="deadline" name="deadline" class="layui-input" type="text" placeholder="yyyy-MM-dd"
                           autocomplete="off" value="${deadline_str}"
                           lay-verify="deadline">
                </td>
            </tr>
            <tr height="23">
                <td align="center">备注:</td>
                <td align="left">
                <textarea name="usercomment" placeholder="请输入备注，可不填" class="layui-textarea"
                          lay-verify="usercomment">${userInfo.usercomment}</textarea>
                </td>
            </tr>
            <tr height="50">
                <td align="center">
                    <button id="formsubmit" class="layui-btn" lay-submit lay-filter="saveroleinfo">更新基本信息</button>
                </td>
            </tr>
        </table>
    </form>
</fieldset>

<fieldset class="layui-elem-field">
    <legend>会员关注课程更新</legend>
    <table class="layui-table" lay-filter="classfilter"
           lay-data="{url:'${pageContext.request.contextPath}/eyas/classinfo/listclassinfo?userid=${userInfo.userid}',page:false,height:'200px', id:'classresult'}">
        <thead>
        <tr>
            <th lay-data="{field:'classid',hide:true}">课程id</th>
            <th lay-data="{field:'classname',align:'center'}">课程名称</th>
            <th lay-data="{fixed: 'right',width:'220',align:'center', toolbar: '#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>
</fieldset>
</body>
<script id="barDemo" type="text/html">
    <input type="checkbox" name="lock" value="{{d.classid}}" title="关注" lay-filter="lockDemo" {{ d.validflag=='1'?'checked':''}}>
</script>
<script type="application/javascript">
    var form;
    layui.use(['form', 'table','laydate'], function () {
        form = layui.form;
        var table = layui.table;

        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#deadline' //指定元素
        });

        //监听锁定操作
        form.on('checkbox(lockDemo)', function(obj){
            var classid = this.value;
            var validflag = '0';
            if(obj.elem.checked)
                validflag = '1';
            $.ajax({
                data : {userid:${userInfo.userid},classid:classid,validflag:validflag},
                type : "PUT",
                url : "${pageContext.request.contextPath}/eyas/userinfo/changeClassFollow",
                dataType : "json",
                success: function(data) {
                    layer.msg("修改成功");

                },
                error:function(xdata){
                    layer.msg(xdata.responseJSON.errormsg);
                }
            });
        });


        //switch off状态赋值
        form.on('switch', function (data) {
            $(data.elem).attr('type', 'hidden').val(this.checked ? '1' : '2');
        });

        //表单验证规则自定义
        form.verify({
            openid: function (value) {
                if (!value) {
                    return 'openid账号不能为空';
                }
            },
            username: function (value) {
                if (!value) {
                    return '用户姓名不能为空';
                }
            },
            telephone: function (value) {
                if (value && value.length > 11) {
                    return '联系电话长度不能超过11位';
                }
            },
            addid: function (value) {
                if (!value) {
                    return '请选择课程顾问';
                }
            }
        });

        //监听提交
        form.on('submit(saveroleinfo)', function (data) {
//            layer.msg(JSON.stringify(data.field));
//            return false;
            $.ajax({
                data: data.field,
                type: "POST",
                url: "${pageContext.request.contextPath}/eyas/userinfo/saveUserInfo",
                dataType: "json",
                success: function (data) {
                    layer.msg("保存成功,页面将在1秒后关闭");
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }, 1000);
                },
                error: function (xdata) {
                    layer.msg(xdata.responseJSON.errormsg);
                }
            });
            return false;
        });
        //加载顾问下拉框
        selectAdminInfo();
    });
    function selectAdminInfo() {
        $.ajax({
            url: "${pageContext.request.contextPath}/eyas/admininfo/queryAdminInfoList",
            type: "GET",
            dataType: "json",
            success: function (result) {
                var list = result;    //返回的数据
                var admin = document.getElementById("addid");        //add_role_name给select定义的id
                for (var i = 0; i < list.length; i++) {
                    var option = document.createElement("option");    // 创建添加option属性
                    option.setAttribute("value", list[i].adminnid);                  // 给option的value添加值
                    if (list[i].adminnid == '${userInfo.addid}')
                        option.setAttribute("selected", "selected");
                    option.innerText = list[i].username;             // 打印option对应的纯文本 （超级管理员、管理员）
                    admin.appendChild(option);                          // 给select 添加option子标签
                }

                form.render("select");                                // 刷性select，显示出数据
            },
            error: function (xdata) {
                layer.msg(xdata.responseJSON.errormsg);
            }
        });
    }
</script>
</html>
